Q: I would like to know how to measure the system time at a specific moment, then at another moment, only a few milliseconds later, to determine elapsed time. 
I have tried clock_gettime(), but this is no good. I have checked the clock resolution and it is at 60 ticks per second. 
A: The following was written by Fred Roeber (froeber@bbn.com) in the newsgroup:

The easiest/best way to measure detailed elapsed time is to use any "timestamp" driver that may be supplied as part of your board's BSP. The timestamp driver is a generic name for a high resolution timing factility that is intended for WindView use but can also be used by user applications. The documentation for creating/using the timestamp facility is in Appendix G of the WindView manual and the functions are defined in h/drv/timer/timerDev.h (see sysTimestamp...).

Many BSPs come with timestamp driver support even if you don't buy WindView. You can add the configuration option INCLUDE_TIMESTAMP to build it into your system. You can then call sysTimestampEnable to start it up and sysTimestampLock to read detailed time values. You can call sysTimestampPeriod to find out how often the timer rolls over and sysTimestampFreq to see the rate it ticks at (which is often fast enough to provide microsecond resolution).

The reason I prefer this approach to cranking up the clock rate is that you typically get much higher resolution values without bogging down the system with all the overhead of frequent timer interrupts.

As with most things in VxWorks, caveats are needed in applying a generic facility to specific boards. First, not all boards have timestamp driver support. Second, on those that do, the timestamp sometimes has a pretty small period (it rolls over frequently). For instance, on the PPC platform, the "standard" timestamp driver derives the timestamps from the decrementer timer which is also used for the system clock. This means the time value rolls over at the rate the system clock rate; making it pretty useless for measuring things with a long duration.

There is an alternative timer for some PPC boards that uses the Z8536 chip. The other day, we needed to time startup code on our Radstone PPC board and I found that with a little work the PPC timestamp functions fit the bill nicely. The ppcZ8536Timer.c can be rewritten such that it only provides auxilliary clock and timestamp support (with no system clock support - that comes from ppcDecTimer.c). By using the 16 bit timers 1 and 2 in "linked" mode it was possible to get a nice 32 bit timer that rolled over every 20 minutes in our case. We left timer 3 on the chip for auxilliary clock support. The original timestamp functionality in the driver was, unfortunately, fairly well broken but not unsalvageable. Just trying to point out that in some (all?) cases you have to be careful when using VxWorks drivers.

Caveats aside, people should really consider the timestamp routines. They can often provide good timing insight with minimum trouble. Fred

